﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SmokinAces.Object_Classes.Objects
{
    //public enum Tree_Type { Tree1 = 1, Tree2 = 2, Tree3 = 3};
    public class Pot : Game_Object, Draw_Object, Logic_Object
    {
        //------this is information for the editor...
        static Type[] constructor_parameters = { typeof(int), typeof(int), typeof(Main_Engine) };
        static object[] default_const_values = { 0, 0, null };
        //------------------------------------
        static Random rand = new Random(7);
        Dictionary<string, int> TexList = new Dictionary<string,int>();
        int TexIndex;
        private Item_Type _currentItem;
        public Item_Type currentItem
        {
            get { return _currentItem; }
            set
            {
                _currentItem = value;
            }
        }
        int maxMoney = 10;
        int maxMana = 10;
        int maxHealth = 10;
        bool hit = false;
        private Sort_Delegate drawSortDelegate;
        VertexPositionColorTexture[] baseImage;
		VertexPositionColorTexture[] vertecies;
		int[] indecies;
       
        public Pot(int left, int top, Main_Engine parent)
        {
            int whatItem = rand.Next(1, 4);
            Item_Type Item = (Item_Type)whatItem;
            Initialize(left, top, left + 32, top + 32, Item, parent);
        }
        public Pot(XmlReader xmlReader, Main_Engine Parent)
        {
            xmlReader.Read();
            string sLeft = xmlReader.ReadElementString("Left");
            string sTop = xmlReader.ReadElementString("Top");
            string sItem = xmlReader.ReadElementString("Item");
            //------------------------------------
            int left = Convert.ToInt32(sLeft);
            int top = Convert.ToInt32(sTop);

            Item_Type Item = (Item_Type)Enum.Parse(typeof(Item_Type), sItem);
            Initialize(left, top, left + 32, top + 32,Item, Parent);
        }
        public override bool Save_XML(XmlTextWriter textWriter)
        {
            try
            {
                textWriter.WriteStartElement("Game_Object");
                textWriter.WriteAttributeString("Type", this.GetType().AssemblyQualifiedName);
                textWriter.WriteElementString("Left", baseImage[0].Position.X.ToString());
                textWriter.WriteElementString("Top", baseImage[0].Position.Y.ToString());
                textWriter.WriteElementString("Item", Enum.GetName(typeof(Item_Type), _currentItem));
                textWriter.WriteEndElement();
                textWriter.Flush();
            }
            catch
            {
                return false;
            }
            return true;
        }
        private void Initialize(int left, int top, int right, int bottom,Item_Type item, Main_Engine parent)
        {
            _currentItem = item;
            const float zorder = 1.0f;
            this.parent = parent;
            GraphicBB = new BoundingBox(new Vector3(left, top, Util.MinZ), new Vector3(right, bottom, Util.MaxZ));
            Color baseColor = new Color(255,255,255,255);
            //color = baseColor;
            baseImage = new VertexPositionColorTexture[4];
            baseImage[0].Position = new Vector3(left, top, zorder);
            baseImage[0].Color = baseColor;
            baseImage[0].TextureCoordinate = new Vector2(1.0f, 1.0f);
            baseImage[1].Position = new Vector3(left, bottom, zorder);
            baseImage[1].Color = baseColor;
            baseImage[1].TextureCoordinate = new Vector2(1.0f, 0.0f);
            baseImage[2].Position = new Vector3(right, top, zorder);
            baseImage[2].Color = baseColor;
            baseImage[2].TextureCoordinate = new Vector2(0.0f, 1.0f);
            baseImage[3].Position = new Vector3(right, bottom, zorder);
            baseImage[3].Color = baseColor;
            baseImage[3].TextureCoordinate = new Vector2(0.0f, 0.0f);

			vertecies = Util.ComplexMesh(baseImage, out indecies, 4, 4);
            TexList.Add("pot", parent.register_texture("Map Objects\\Pot.png"));
            TexList.Add("money", parent.register_texture("gui\\Money.png"));
            TexList.Add("health", parent.register_texture("gui\\health.png"));
            TexList.Add("mana", parent.register_texture("gui\\mana.png"));
            TexIndex = TexList["pot"];
            drawSortDelegate = new Sort_Delegate(((1024 - (top + 8)) * 1024) + left , Draw);
            Bounding_Regions.Add(new BoundingBox(new Vector3(left + 12, top + 9, -1), new Vector3(left + 25, top + 18, 1)));
        }

        #region Draw_Object Members

        public bool isMoney()
        {
            return TexList["money"] == TexIndex;
        }
        public int getMoney()
        {
            return maxMoney;
        }
        public bool isMana()
        {
            return TexList["mana"] == TexIndex;
        }
        public int getMana()
        {
            return maxMana;
        }
        public bool isHealth()
        {
            return TexList["health"] == TexIndex;
        }
        public int getHealth()
        {
            return maxHealth;
        }

        public void Draw(GameTime gameTime, GraphicsDevice device)
        {
                //---Set the Texture for this layer
                parent.effect.BaseTexture = parent.Texture_List[TexIndex];
                //---------Begin the drawing passes
                parent.effect.Begin();
                foreach (EffectPass pass in parent.effect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    //device.DrawUserPrimitives(PrimitiveType.TriangleStrip, baseImage, 0, 2);
                    device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertecies, 0, vertecies.Length, indecies, 0, indecies.Length / 3);
                    pass.End();
                }
                parent.effect.End();
        }
        public bool startDraw()
        {
            parent.Draw_Functions[3].Add(drawSortDelegate);
            return true;
        }
        public bool stopDraw()
        {
            parent.Draw_Functions[3].Remove(drawSortDelegate);
            return true;
        }
        #endregion

        #region Game_Object Members

        public override bool dispose()
        {
            //----------------I used some basic boolean logic here...
            stopDraw();
            return parent.Draw_Functions[3].Remove(drawSortDelegate);
            //return parent.remove_Drawing_Object("Tree\\Tree" + ((int)currentTree) + ".png", Draw, Layers.Layer3);
        }
        #endregion


        #region Logic_Object Members

        public void Update(GameTime gameTime)
        {
            HashSet<Game_Object> crash = this.isCollidingWith(Bounding_Regions[0], Collision_Type.Weapon);
            if (crash.Count > 0 && !hit)
            {
                SoundManager.PlaySound("PotBreak");
                Item i = new Item((int)((GraphicBB.Max.X - GraphicBB.Min.X) / 2.0f + GraphicBB.Min.X) - 10, (int)((GraphicBB.Max.Y - GraphicBB.Min.Y) / 2.0f + GraphicBB.Min.Y) - 10, _currentItem, parent);
                parent.MapObjects.Remove(this);
                parent.MapObjects.Add(i);
            }
        }
        public bool stopUpdate()
        {
            parent.remove_Update_Object(Update);
            return true;
        }

        public bool startUpdate()
        {
            parent.add_Update_Object(Update);
            return true;
        }

        #endregion
    }
}
